package com.smartstate.iot.service.impl;

import com.alibaba.excel.EasyExcel;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.iot.entity.TopicConfig;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.mapper.TopicLogMapper;
import com.smartstate.iot.mapper.TopicMapper;
import com.smartstate.iot.service.TopicService;
import com.smartstate.iot.vo.TopicUpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class TopicImpl extends ServiceImpl<TopicMapper, TopicConfig> implements TopicService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    private TopicLogMapper topicLogMapper;


    /**
     * 查询redis中存在多少个设备(key)
     *
     * @return
     */


    @Override
    public Integer findAllDevice() {
        ArrayList<String> device = getKeys();
        int size = device.size();
        return size;
    }

    /**
     * 判断当前信息是新设备  还是已经存在设备
     *
     * @param content
     * @return
     */
    @Override
    public Boolean logOnly(TopicUpVo.Content content) {
        if (topicMapper.getIpPort(content) >= 1) {
//           数据库中大于一条代表该设备已经存在
            return false;
        } else
//      小于一条代表是新设备
            return true;
    }

    /**
     * 更新盒子心跳信息
     */
    @Override
    public Boolean updateConfig(TopicConfig topicConfig, TopicUpVo.Content content) {
        return topicMapper.updateByPortIp(topicConfig, content);
    }

    /**
     * 查询这个当前端口的上次心跳信息时间戳
     */
    @Override
    public TopicConfig getUpTime(TopicConfig topicConfig) {
        TopicConfig upTime = topicMapper.getUpTime(topicConfig);
        return upTime;
    }

    @Override
    public List<TopicConfig> getNormalBoxes() {
        return topicMapper.getNormalBoxes();
    }

    /**
     * 查询网管设备总数
     */
    @Override
    public List<Integer> getAllType() {
        ArrayList<Integer> allTypeList = new ArrayList<>();
        LinkedList<String> typeList = new LinkedList<>();
        List<String> allType = topicMapper.getAllType();
        int size = allType.size();
        for (String s : allType) {
            String typeSize = topicLogMapper.getTypeSize(s);
            typeList.add(typeSize);
        }
        int ok = 0;
        int off = 0;
        int on = 0;
        for (String s : typeList) {
            if ("正常".equals(s)) {
                ok++;
            } else if ("异常".equals(s)) {
                off++;
            } else {
                on++;
            }
        }
        allTypeList.add(ok);
        allTypeList.add(on);
        allTypeList.add(off);
        allTypeList.add(size);
        return allTypeList;
    }

    @Override
    public String getTypeByDeviceId(String deviceId) {
        TopicLog topicLog = topicLogMapper.getTypeByDeviceId(deviceId);
        String type = topicLog.getType();
        return type;
    }

  /*  @Override
    public void saveByDeviceId(TopicUpVo topicUpVo) {
        TopicConfig topicConfig = new TopicConfig();
        TopicLog topicLog = new TopicLog();
        TopicConfig topicConfig1 = topicMapper.getByDeviceId(topicUpVo.getContent().get(0).getDeviceId());
        if (topicConfig1 == null) {
            TopicUpVo.Content content = topicUpVo.getContent().get(0);
            topicConfig.setConfig(topicUpVo.getConfig());
            topicConfig.setTimesTamps(topicUpVo.getTimesTamp());
            topicConfig.setNewTopic(content.getNewTopic());
            topicConfig.setDeviceName(content.getDeviceName());
            topicConfig.setCpu(content.getCpu());
            topicConfig.setMomery(content.getMomery());
            topicConfig.setDeviceType(content.getDeviceType());
            topicConfig.setDeviceId(content.getDeviceId());
            topicConfig.setIp(content.getIp());
            topicConfig.setPort(content.getPort());
            topicMapper.insert(topicConfig);
            topicLog.setDeviceId(content.getDeviceId());
            topicLog.setLogName(content.getDeviceName());
            topicLog.setDate(new DateTime().toString());
            if ((content.getDeviceId() != "" && content.getDeviceId() != null) &&
                    (content.getDeviceName() != "" && content.getDeviceName() != null) &&
                    (content.getIp() != "" && content.getIp() != null) &&
                    (content.getPort() != "" && content.getPort() != null) &&
                    (content.getCpu() != "" && content.getCpu() != null) &&
                    (content.getMomery() != "" && content.getMomery() != null)
            ) {
                topicLog.setType("正常");
                topicLog.setDetail("网关状态正常！");
            } else {
                topicLog.setType("异常");
                topicLog.setDetail("网关状态异常！");
            }
            topicLogMapper.insert(topicLog);
        } else {
            TopicUpVo.Content content = topicUpVo.getContent().get(0);
            topicConfig.setConfig(topicUpVo.getConfig());
            topicConfig.setTimesTamps(topicUpVo.getTimesTamp());
            topicConfig.setNewTopic(content.getNewTopic());
            topicConfig.setDeviceName(content.getDeviceName());
            topicConfig.setCpu(content.getCpu());
            topicConfig.setMomery(content.getMomery());
            topicConfig.setDeviceType(content.getDeviceType());
            topicConfig.setDeviceId(content.getDeviceId());
            topicConfig.setIp(content.getIp());
            topicConfig.setPort(content.getPort());
            topicMapper.update(topicConfig, new LambdaQueryWrapper<TopicConfig>().eq(TopicConfig::getDeviceId, topicConfig.getDeviceId()));
            topicLog.setDeviceId(content.getDeviceId());
            topicLog.setLogName(content.getDeviceName());
            topicLog.setDate(new DateTime().toString());
            if ((content.getDeviceId() != "" && content.getDeviceId() != null) &&
                    (content.getDeviceName() != "" && content.getDeviceName() != null) &&
                    (content.getIp() != "" && content.getIp() != null) &&
                    (content.getPort() != "" && content.getPort() != null) &&
                    (content.getCpu() != "" && content.getCpu() != null) &&
                    (content.getMomery() != "" && content.getMomery() != null)
            ) {
                TopicLog type = topicLogMapper.getTypeByDeviceId(topicLog.getDeviceId());
                if (!"正常".equals(type.getType())) {
                    topicLog.setType("正常");
                    topicLog.setDetail("网关状态正常！");
                    topicLogMapper.insert(topicLog);
                }
            } else {
                TopicLog type = topicLogMapper.getTypeByDeviceId(topicLog.getDeviceId());
                if (!"异常".equals(type.getType())) {
                    topicLog.setType("异常");
                    topicLog.setDetail("网关状态异常！");
                    topicLogMapper.insert(topicLog);
                }
            }
        }
    }
*/

    /**
     * 获取redis中所有的key
     *
     * @return
     */
    private ArrayList<String> getKeys() {
        List<TopicLog> topicLogs = topicLogMapper.getAllDevice();
        TreeSet<String> boxName = new TreeSet<>();
        for (TopicLog topicLog : topicLogs) {
            boxName.add(topicLog.getDeviceId());
        }
        System.out.println("boxName = " + boxName);
        //  获取所有的key
        ArrayList<String> keys = new ArrayList<>();
        for (String logSet : boxName) {
            Set<String> tempKeys = redisTemplate.keys(logSet + ":*");
            if (tempKeys != null) {
                keys.addAll(tempKeys);
            }
        }
        System.out.println("keys = " + keys);
        return keys;
    }


}
